package com.yc.cloud.admin.service.impl;

import cn.hutool.core.map.MapUtil;
import com.yc.cloud.admin.service.UmsAdminCacheService;
import com.yc.cloud.api.constants.RedisKeyConstants;
import com.yc.cloud.api.utils.MyFastJsonUtils;
import com.yc.cloud.common.cache.service.RedisService;
import com.yc.cloud.entity.UmsAdmin;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UmsAdminCacheServiceImpl implements UmsAdminCacheService {
    @Resource
    private RedisService<UmsAdmin> redisService;

//    @Override
//    public void delResourceListByRole(Long roleId) {
//        val queryMap = new HashMap<String, Object>();
//        queryMap.put("role_id", roleId);
//        List<UmsAdminRoleRelation> relationList = adminRoleRelationMapper.selectByMap(queryMap);
//        if (CollUtil.isNotEmpty(relationList)) {
//            String keyPrefix = RedisKeyConstants.ADMIN_RESOURCE;
//            List<String> keys = relationList.stream().map(relation -> keyPrefix + relation.getAdminId()).collect(Collectors.toList());
//            redisService.del(keys);
//        }
//    }
//
//    @Override
//    public void delResourceListByRoleIds(List<Long> roleIds) {
//        List<UmsAdminRoleRelation> relationList = adminRoleRelationMapper.selectBatchIds(roleIds);
//        if (CollUtil.isNotEmpty(relationList)) {
//            String keyPrefix = RedisKeyConstants.ADMIN_RESOURCE;
//            List<String> keys = relationList.stream().map(relation -> keyPrefix + relation.getAdminId()).collect(Collectors.toList());
//            redisService.del(keys);
//        }
//    }
//
//    @Override
//    public void delResourceListByResource(Long resourceId) {
//        List<Long> adminIdList = umsAdminMapper.getAdminIdList(resourceId);
//        if (CollUtil.isNotEmpty(adminIdList)) {
//            String keyPrefix = RedisKeyConstants.ADMIN_RESOURCE;
//            List<String> keys = adminIdList.stream().map(adminId -> keyPrefix + adminId).collect(Collectors.toList());
//            redisService.del(keys);
//        }
//    }

//    @Override
//    public List<UmsResource> getResourceList(String username) {
//        String key = getUserNameRealKey(RedisKeyConstants.ADMIN_RESOURCE,username);
//        Object object = redisService.get(key);
//        if (object != null) {
//            return (List<UmsResource>) object;
//        }
//        return new ArrayList<>();
//    }

//    @Override
//    public void setResourceList(String username, List<UmsResource> resourceList) {
//        String key = RedisKeyConstants.ADMIN_RESOURCE + adminId;
//        redisService.set(key, resourceList, expire);
//    }

    @Override
    public void delByUserName(String username) {
        val key = getUserNameRealKey(username);
        delByKey(key);
    }

    @Override
    public UmsAdmin getByUserName(String username) {
        val key = getUserNameRealKey(username);
        return getByKey(key);
    }

    @Override
    public void setByUserName(String username, UmsAdmin umsAdmin) {
        val key = getUserNameRealKey(username);
        setByKey(key, umsAdmin);
    }


    @Override
    public UmsAdmin getByKey(String key) {
        val adminAccountMap = redisService.hGetAll(key);
        if (MapUtil.isNotEmpty(adminAccountMap)) {
            val umsAdmin = (UmsAdmin) MyFastJsonUtils.map2Object(adminAccountMap, UmsAdmin.class);
            if (umsAdmin != null && umsAdmin.getId() != null) {
                return umsAdmin;
            }
        }
        return null;
    }

    @Override
    public void setByKey(String key, UmsAdmin umsAdmin) {
        //对象转map
        val map = MyFastJsonUtils.object2Map(umsAdmin);
        redisService.hSetAll(key, map, expire);
    }

    @Override
    public void setByKey(String key, UmsAdmin umsAdmin, long expire) {

    }

    @Override
    public boolean existByKey(String key) {
        return redisService.hasKey(key);
    }

    @Override
    public List<UmsAdmin> listByKey(String key, long pageNum, long pageSize) {
        return null;
    }

    @Override
    public void delByKey(String key) {
        redisService.del(key);
    }

    @Override
    public List<UmsAdmin> listAllByKey(String key) {
        return null;
    }

    @Override
    public void lPushAllByCollectionByKey(String key, List<UmsAdmin> list) {

    }

    @Override
    public UmsAdmin get(Long id) {
        return null;
    }

    @Override
    public void set(Long id, UmsAdmin umsAdmin) {

    }

    @Override
    public boolean exist(Long id) {
        return false;
    }

    @Override
    public List<UmsAdmin> list(long pageNum, long pageSize) {
        return null;
    }

    @Override
    public List<UmsAdmin> listAll() {
        return null;
    }

    @Override
    public void del(Long id) {

    }

    private String getUserNameRealKey(String username) {
        return RedisKeyConstants.ADMIN_ACCOUNT.replace(RedisKeyConstants.USERNAME, username);
    }


}
